﻿//Frank Baumgartner sample-accurate timing hack - hopefully will port to a class (Scope problems)
//-----------------------------------------------------------------------------------------------

//variables necessary for scope of recursive onsoundcomplete function (a bit iffy - but scoping problems with timing hack as a class)
var bpm:Number = 123.2; //bpm
var SPD:Number = 6; //throttle on ticks 12 is normal speed
var tmr:Number = 0;
var stp:Number =0; 
var len:Number = 64; //length of sequence
var seq:Array = new Array(len); // container for a seq of logo explosions to beats

//map beats in sequence
//seq[0] = seq[3] = seq[4] = seq[10] = seq[14] = seq[18] = seq[22] = seq[26] = seq[30] = seq[34] = seq[38] = seq[42] = 1//= seq[46] = seq[48] = seq[50] = seq[52] = seq[56] = seq[58] = seq[60] = 1;
//only the stabs and snares
seq[0] = seq[4] = seq[5] = seq[13] = seq[21] = seq[29] = seq[33] = seq[37] = seq[45] = seq[52] = seq[53] = seq[61] = 1;
			
		
//initialise the silent 1sample clip
createEmptyMovieClip("sync_mc", 3000);

//-loadsounds-----------------------------------------------------------

	var sync:Sound = new Sound(sync_mc);
	sync.attachSound("sync");
	sync.setVolume(0);
	
	var title_music = new Sound(this);
	title_music.attachSound("Title_loop");
	
//---------------------------------------------------------------------

sync.onSoundComplete = function() //sync the sample playback according to the amount of buffers which have elapsed 
{
	sync.start(0, 1); //44100*SPD/(BPM*2/5) - 44100*23.2*3/1000 );
	
	tmr -= 2048/44100; // buffers out of a second (in samples)
	
	if (tmr > 0) {return;}; //kick back untill the alloted tick has elapsed
	
	tmr += SPD/(bpm*2/5); // tick value for tracker (in samples)
	var offset:Number = SPD/(bpm*2/5) - tmr; //amount to offset sample by according to gap in accuracy between the projected and actual tick value in samples.
	
	if(stp == 0)
	{
		title_music.start(offset,1);
	}
	
	if(seq[stp] == 1)
	{
		_root.explode(); //throw points out
	}
	else
	{
		_root.rumble(); //move a small amount (simulate other frequencies)
	}
	
	stp = (stp + 1) % len; //increment the seqr stp
}
sync.start(0, 1); // kick off recursion

//-----------------------------------------------------------------------------------------------

kill_sequencer = function()
{
	trace("killing off sequencer");
	delete sync.onSoundComplete;//kill the complete function
	sync_mc.removeMovieClip(); //kill clip and .: timing loop .: stop title music
	//kill all variables used
	delete seq; //kill array
	delete bpm; 
	delete SPD; 
	delete tmr;
	delete stp; 
	delete len;
}